package Offer;

import org.junit.Test;

public class T17_printNumbers {
    @Test
    public void test() {
        printNumbers(1);
    }

    // 1 效率低，主要消耗还是在pow()函数
    public int[] printNumbers(int n) {
        int len = (int) Math.pow(10, n) - 1;
        int[] nums = new int[len];
        int index = 1;  //从1开始输出
        int count = 1;  //记录位数
        while (count <= n) {
            nums[index-1] = index;
            index++;
            if (index == (int) Math.pow(10, count)) { //记录位数
                count++;
            }
        }
        return nums;
    }

    // 2 超过100%
    public int[] printNumbers_1(int n) {
        int len = (int) Math.pow(10, n) - 1;
        int[] nums = new int[len];
        int index = 1;  //从1开始输出
        int max = (int) Math.pow(10, n);
        while (index < max) {
            nums[index - 1] = index;
            index++;
        }
        return nums;
    }
}
